/*
 * @Project Name: shoplook
 * @File Name: FrontPageController
 * @Package Name: com.sm.shoplook.controller.front
 * @Date: 2018/8/15 10:39
 * @Creator: linshixing-1199
 * @line------------------------------
 * @修改人:
 * @修改时间:
 * @修改内容:
 */

package com.sm.shoplook.controller.front;

import com.github.pagehelper.PageInfo;
import com.sm.shoplook.constant.ResultMessage;
import com.sm.shoplook.domain.base.MainIndex;
import com.sm.shoplook.domain.base.Material;
import com.sm.shoplook.domain.base.MaterialCategory;
import com.sm.shoplook.domain.customer.Customer;
import com.sm.shoplook.domain.service.SlService;
import com.sm.shoplook.service.about.AboutUsService;
import com.sm.shoplook.service.about.PartnerCaseService;
import com.sm.shoplook.service.base.MainIndexService;
import com.sm.shoplook.service.base.MaterialCategoryService;
import com.sm.shoplook.service.base.MaterialService;
import com.sm.shoplook.service.customer.ApiCustomerService;
import com.sm.shoplook.service.service.SlSerService;
import com.sm.shoplook.service.star.StarService;
import com.sm.shoplook.service.suit.ApiSuitCommentService;
import com.sm.shoplook.service.suit.ApiSuitScanRecordService;
import com.sm.shoplook.service.suit.ApiSuitService;
import com.sm.shoplook.vo.base.ApiException;
import com.sm.shoplook.vo.base.ApiResult;
import com.sm.shoplook.vo.discover.req.DisCoverPageSearchVo;
import com.sm.shoplook.vo.service.req.SlServicePageSearchReqVo;
import com.sm.shoplook.vo.star.req.StarSearchReqVo;
import com.sm.shoplook.vo.star.resp.StarRespVo;
import com.sm.shoplook.vo.suit.front.resp.ApiSuitListRespVo;
import com.sm.shoplook.vo.suit.front.resp.SuitPageRespVo;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author chenpy
 * @desc 前端页面控制器
 * @date 2018/8/15 10:39
 * @see
 */
@Controller
@RequestMapping("/front")
public class FrontPageController {

	private static final Logger LOGGER = LoggerFactory.getLogger(FrontPageController.class);
	@Autowired
	private MaterialService materialService;
	@Autowired
	private AboutUsService aboutUsService;
	@Autowired
	private PartnerCaseService partnerCaseService;
	@Autowired
	private SlSerService slSerService;
	@Autowired
	private ApiSuitService apiSuitService;
	@Autowired
	private ApiCustomerService apiCustomerService;
	@Autowired
	private MaterialCategoryService materialCategoryService;
	@Autowired
	private ApiSuitScanRecordService apiSuitScanRecordService;
	@Autowired
	private ApiSuitCommentService apiSuitCommentService;
	@Autowired
	private StarService starService;
	@Autowired
	private MainIndexService mainIndexService;

	@RequestMapping("/index")
	public String index() {
		return "/front/inspire";
	}

	@RequestMapping("/star")
	public String star() {
		return "/front/star";
	}

	@RequestMapping(value = "/user-info-create", method = RequestMethod.GET)
	public String userInfoCreate(Model model) {
		ApiResult<List<MaterialCategory>> listApiResult = materialCategoryService.selectAllMaterialCategorys();
		List<MaterialCategory> materialCategories = listApiResult.getData();
		if (CollectionUtils.isEmpty(materialCategories)) {
			materialCategories = new ArrayList<>();
		}
		model.addAttribute("materialCategories", materialCategories);
		return "/front/user-info-create";
	}

	@ResponseBody
	@RequestMapping(value = "/selectMaterialsByCategoryId", method = RequestMethod.GET)
	public ApiResult<List<Material>> selectMaterialsByCategoryId(Integer categoryId) {
		return materialService.selectMaterialsByCategoryId(categoryId);
	}

	@RequestMapping(value = "/about", method = RequestMethod.GET)
	public String about() {
		return "/front/about";
	}

	@RequestMapping(value = "/discover", method = RequestMethod.GET)
	public String discover() {
		return "/front/discover";
	}

	@RequestMapping(value = "/service", method = RequestMethod.GET)
	public String service() {
		return "/front/service";
	}

	/**
	 * 关于我们页面数据
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/about_data")
	public ApiResult<Map<String, Object>> aboutData() {
		Map<String, Object> resultMap = new HashMap<>();
		resultMap.put("allAboutUs", aboutUsService.selectAllAboutUs().getData());
		// 成功案例
		resultMap.put("successCase", partnerCaseService.selectPartnerCaseByType(1, 0, 4).getData());
		// 合作伙伴
		resultMap.put("partners", partnerCaseService.selectPartnerCaseByType(2, 0, 4).getData());
		return new ApiResult<>(ResultMessage.RESULT_SUCCESS_1, resultMap);
	}

	/**
	 * 服务页面数据
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/service_data")
	public ApiResult serviceData(Integer pageNo, Integer pageSize) {
		SlServicePageSearchReqVo slServicePageSearchReqVo = new SlServicePageSearchReqVo();
		slServicePageSearchReqVo.setOffset(pageNo);
		slServicePageSearchReqVo.setPageSize(pageSize);
		slServicePageSearchReqVo.setSortOrder("DESC");
		slServicePageSearchReqVo.setSort("id");
		return new ApiResult<>(ResultMessage.RESULT_SUCCESS_1, slSerService.selectSlServicePage(
				slServicePageSearchReqVo).getData());
	}

	/**
	 * 发现页面数据
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/discover_data")
	public ApiResult discoverData(Integer pageNo, Integer pageSize, DisCoverPageSearchVo disCoverPageSearchVo) {
		disCoverPageSearchVo.setOffset(pageNo);
		disCoverPageSearchVo.setPageSize(pageSize);
		disCoverPageSearchVo.setSortOrder("DESC");
		disCoverPageSearchVo.setSort("id");
		if (null != disCoverPageSearchVo.getCategory() && disCoverPageSearchVo.getCategory() < 1) {
			disCoverPageSearchVo.setCategory(null);
		}
		return new ApiResult<>(ResultMessage.RESULT_SUCCESS_1, apiSuitService.selectSuitePage(disCoverPageSearchVo)
				.getData());
	}

	/**
	 * 素材数据
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/material_data")
	public ApiResult materialData() {
		return materialService.selectAllMaterials();
	}

	/**
	 * @Author chenpy
	 * @Date 2018/8/21 15:43
	 * @Description 跳转服务详情页
	 * @return
	 */
	@RequestMapping(value = "/service_detail/{id}", method = RequestMethod.GET)
	public String serviceDetail(@PathVariable(name = "id") Integer id, Model model) {
		ApiResult<SlService> slServiceApiResult = slSerService.selectSlServiceById(id);
		SlService slService = slServiceApiResult.getData();
		if (null == slService) {
			slService = new SlService();
		}
		model.addAttribute("service", slService);
		return "/front/service_detail";
	}

	/**
	 * 我的个人作品
	 * @param customerId 会员ID
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/selectSuitByCustomerId", method = RequestMethod.GET)
	public ApiResult<List<ApiSuitListRespVo>> selectSuitByCustomerId(Integer customerId) {
		return apiSuitService.selectCategorySuitByCustomerId(customerId);
	}

	/**
	 * 用户详情
	 * @param customerId 会员ID
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/user_info/{customerId}/{status}", method = RequestMethod.GET)
	public String userInfo(@PathVariable(name = "customerId") Integer customerId, @PathVariable Integer status,
			Model model) {
		DisCoverPageSearchVo disCoverPageSearchVo = new DisCoverPageSearchVo();
		disCoverPageSearchVo.setCustomerId(customerId);
		disCoverPageSearchVo.setPageNo(0);
		disCoverPageSearchVo.setPageSize(100);
		if (status == 0) {
			status = null;
		}
		disCoverPageSearchVo.setStatus(status);
		ApiResult<PageInfo<SuitPageRespVo>> pageInfoApiResult = apiSuitService
				.selectAllSuitByCustomerId(disCoverPageSearchVo);
		PageInfo<SuitPageRespVo> data = pageInfoApiResult.getData();
		List<SuitPageRespVo> suitListRespVos = data.getList();
		if (CollectionUtils.isEmpty(suitListRespVos)) {
			suitListRespVos = new ArrayList<>();
		}
		model.addAttribute("suitListRespVos", suitListRespVos);
		ApiResult<Map<String, Object>> mapApiResult = apiSuitService.statisCustomerSuits(customerId, null, status);
		Map<String, Object> mapApiResultData = mapApiResult.getData();
		if (MapUtils.isEmpty(mapApiResultData)) {
			mapApiResultData = new HashMap<>();
		}
		model.addAttribute("statisData", mapApiResultData);
		ApiResult<Customer> customerApiResult = apiCustomerService.selectCustomerById(customerId);
		Customer customer = customerApiResult.getData();
		if (null == customer) {
			customer = new Customer();
		}
		model.addAttribute("customer", customer);
		return "/front/user-info";
	}

	/**
	 * 套装详情
	 * @param customerId 会员ID
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/user_info_detail/{customerId}/{suitId}", method = RequestMethod.GET)
	public String userInfoDetail(@PathVariable(name = "suitId") Integer suitId,
			@PathVariable(name = "customerId") Integer customerId, Model model) {
		// 套装信息
		ApiResult<Map<String, Object>> mapApiResult = apiSuitService.statisCustomerSuits(customerId, suitId, null);
		Map<String, Object> mapApiResultData = mapApiResult.getData();
		if (MapUtils.isEmpty(mapApiResultData)) {
			mapApiResultData = new HashMap<>();
		} else {
			String materialIds = (String) mapApiResultData.get("material_ids");
			String[] materialIdsArray = materialIds.split(",");
			List<Integer> mIds = new ArrayList<>();
			if (ArrayUtils.isNotEmpty(materialIdsArray)) {
				for (String s : materialIdsArray) {
					mIds.add(Integer.parseInt(s));
				}
				ApiResult<List<Material>> materialResult = materialService.selectMaterialsByIds(mIds);
				model.addAttribute("materialIds", materialResult.getData());
			}
			Integer auditStatus = (Integer) mapApiResultData.get("audit_status");
			String statusCn = "";
			if (auditStatus == 0) {
				statusCn = "（待审核）";
			} else if (auditStatus == 2) {
				statusCn = "（审核不通过）";
			}
			mapApiResultData.put("statusCn", statusCn);
		}
		model.addAttribute("statisData", mapApiResultData);
		// 评论数
		ApiResult<Integer> suitCommentCount = apiSuitCommentService.getSuitCommentCount(suitId);
		model.addAttribute("suitCommentCount", suitCommentCount.getData());
		ApiResult<Customer> customerApiResult = apiCustomerService.selectCustomerById(customerId);
		Customer customer = customerApiResult.getData();
		if (null == customer) {
			customer = new Customer();
		}
		model.addAttribute("customer", customer);
		return "/front/user-info-detail";
	}

	/**
	 * 是否收藏过该作品
	 * @param suitId
	 * @param customerId
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/isCollectedSuit", method = RequestMethod.GET)
	public ApiResult isCollectedSuit(Integer suitId, Integer customerId) {
		return apiSuitScanRecordService.getCollectSuitCond(customerId, suitId);
	}

	/**
	 * 取消 shoucang
	 * @param suitId 套装ID
	 * @param customerId 会员ID
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/removeCollect", method = RequestMethod.POST)
	public ApiResult removeCollect(Integer suitId, Integer customerId) {
		try {
			return apiSuitScanRecordService.removeCollect(suitId, customerId);
		} catch (ApiException e) {
			e.printStackTrace();
			LOGGER.error("e: {}", e);
			return new ApiResult<>(ResultMessage.RESULT_ERROR_0);
		}
	}

	/**
	 * 明星款页面数据
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/star_data")
	public ApiResult starData(Integer pageNo, Integer pageSize) {
		StarSearchReqVo starSearchReqVo = new StarSearchReqVo();
		starSearchReqVo.setOffset(pageNo);
		starSearchReqVo.setPageSize(pageSize);
		starSearchReqVo.setSortOrder("DESC");
		starSearchReqVo.setSort("id");
		return new ApiResult<>(ResultMessage.RESULT_SUCCESS_1, starService.selectStarDesignPage(starSearchReqVo)
				.getData());
	}

	/**
	 * @param id
	 * @param categoryId
	 * @return
	 * @Author chenpy
	 * @Description
	 * @Date 明星款数据
	 */
	@ResponseBody
	@RequestMapping(value = "/getStarById", method = RequestMethod.GET)
	public ApiResult<Map<String, Object>> getStarById(@RequestParam(name = "id") Integer id,
			@RequestParam(name = "categoryId") Integer categoryId) {
		LOGGER.info("id: {}", id);
		LOGGER.info("categoryId: {}", categoryId);
		Map<String, Object> resultMap = new HashMap<>(2);
		StarSearchReqVo starSearchReqVo = new StarSearchReqVo();
		starSearchReqVo.setCategoryId(categoryId);
		starSearchReqVo.setSort("id");
		starSearchReqVo.setSortOrder("DESC");
		starSearchReqVo.setOffset(0);
		starSearchReqVo.setPageSize(4);
		List<StarRespVo> list = starService.selectStarDesignsByCategoryId(starSearchReqVo).getData().getList();
		StarRespVo data = starService.selectStarDesignById(id).getData();
		resultMap.put("list", list);
		resultMap.put("data", data);
		return new ApiResult<>(ResultMessage.RESULT_SUCCESS_1, resultMap);
	}

	/**
	 * @param type 类型： 1特色 2时装潮流
	 * @param count 数量
	 * @return
	 * @Author chenpy
	 * @Description
	 * @Date 首页
	 */
	@ResponseBody
	@RequestMapping(value = "/getMainIndex", method = RequestMethod.GET)
	public ApiResult<List<MainIndex>> getMainIndex(@RequestParam(name = "type") Integer type,
			@RequestParam(name = "count") Integer count) {
		LOGGER.info("id: {}", type);
		LOGGER.info("count: {}", count);
		return mainIndexService.getFrontMainIndex(type, count);
	}

	/**
	 * 查看更多作品分页
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/getSuitsPage", method = RequestMethod.GET)
	public ApiResult getSuitsPage(DisCoverPageSearchVo disCoverPageSearchVo) {
		ApiResult<PageInfo<SuitPageRespVo>> pageInfoApiResult = apiSuitService
				.selectAllSuitByCustomerId(disCoverPageSearchVo);
		PageInfo<SuitPageRespVo> data = pageInfoApiResult.getData();
		return new ApiResult<>(ResultMessage.RESULT_SUCCESS_1, data);
	}

	/**
	 * 我的收藏
	 * @return
	 */
	@RequestMapping(value = "/collect-list/{customerId}", method = RequestMethod.GET)
	public String collectListPage() {
		return "/front/collect-list";
	}

	@ResponseBody
	@RequestMapping(value = "/collectData", method = RequestMethod.GET)
	public ApiResult collectData(Integer customerId) {
		return apiSuitService.getMyCollect(customerId);
	}
}
